package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.Concest;

public class Link extends NokaTag{
	private static final long serialVersionUID = -3543739719287623807L;
	private String href = null;//
	private String type = "text/css";
	private String rel = null;
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		try {
			if(null==request.getAttribute(href)){
				StringBuffer sb = new StringBuffer();
				if(href.startsWith("/"))
					sb.append("<link type=\""+type+"\" href=\""+basepath(request)+href+(href.indexOf("?")==-1?"?":"&")+"v="+Concest.SYSTEM_START_TIME.getTime()+"\" "+(null==rel?"":"rel=\""+rel+"\"")+"/>");
				else if(href.startsWith("http"))
					sb.append("<link type=\""+type+"\" href=\""+href+(href.indexOf("?")==-1?"?":"&")+"v="+Concest.SYSTEM_START_TIME.getTime()+"\" "+(null==rel?"":"rel=\""+rel+"\"")+"/>");
				else
					sb.append("<link type=\""+type+"\" href=\""+basepath(request)+"/"+href+(href.indexOf("?")==-1?"?":"&")+"v="+Concest.SYSTEM_START_TIME.getTime()+"\" "+(null==rel?"":"rel=\""+rel+"\"")+"/>");
				jspOut.write(sb.toString());
				request.setAttribute(href,"true");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}


	public String getHref() {
		return href;
	}


	public void setHref(String href) {
		this.href = href;
	}
}
